package com.isunland.managesystem.ui;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.AttendanceListAdapter;
import com.isunland.managesystem.adapter.AttendanceListAdapter.Callback;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.AttendanceDetail;
import com.isunland.managesystem.entity.AttendanceListOriginal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SignFielListFragment extends BaseListFragment implements Callback {

  private AttendanceListAdapter mAdapter;

  private List<AttendanceDetail> mAttendanceDetail;

  @Override public String getUrl() {

    return ApiConst.URL_SIGN_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("page", 1 + "");
    params.put("rows", 1000 + "");
    params.put("checkType", "keep");
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    AttendanceListOriginal attendanceListOriginal =
        new Gson().fromJson(arg0, AttendanceListOriginal.class);
    List<AttendanceDetail> attendanceDetail = attendanceListOriginal.getList();
    setAdapter(attendanceDetail);
  }

  private void setAdapter(List<AttendanceDetail> attendaceDetails) {
    if (mAttendanceDetail == null) {
      mAttendanceDetail = new ArrayList<>();
    }
    mAttendanceDetail.clear();
    mAttendanceDetail.addAll(attendaceDetails);
    if (mAdapter == null) {
      mAdapter = new AttendanceListAdapter(getActivity(), mAttendanceDetail, this,
          AttendanceListAdapter.TYPE_USER);
      setListAdapter(mAdapter);
    }
    ((AttendanceListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_refresh, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;

      case R.id.menu_item_refresh:
        volleyPost();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public void refresh() {
    volleyPost();
  }

  @Override public void click(View v) {
    // Auto-generated method stub
  }
}
